
define(function(require,exports,module){
	var Base={
		Index_index:function(){
		},
		Article_index:function(){
			var coverColor=['DB4453','248DE9','242E38','00A377'];
			$('.thumbnail img[src="/static/cover.png"]').each(function(){
				var n=parseInt(Math.random()*(coverColor.length-1));
				$(this).css('background','#'+coverColor[n]);
			});
		},
		Article_read:function(){
			require('/static/plugins/prettify/src/prettify.min');
			require('/static/plugins/zclip/jquery.zclip.min');
			var article_id=$('article.post').attr('data-id');
			window.SSSUIcodeSource=[];
			$('pre').each(function(i,v){
				var h=$(this).html();
				h=h.replace(/&nbsp;/ig,' ');
				//console.info(h);
				$(this).html(h);
				SSSUIcodeSource[i]=h;
			});
			prettyPrint();
			$('pre').each(function(i,v){
				var self = $(this);
				var copy = $('<div><span>复制代码</span></div>')
					.addClass('SSSUI-code-copy')
					.appendTo(self)
					.zclip({
						'path':'/static/plugins/zclip/ZeroClipboard.swf',
						'afterCopy' : function(){
	                        var d = dialog({content:'复制成功！'}).show();
	                        setTimeout(function () {d.close().remove();}, 1000);
						},
						'copy': function(){
							return SSSUIcodeSource[i];
						}
					});
			});
	        require('dialog');
	        SSSUI.addkeyListener('e',{alt:1,cb:function(){
				if(uid>0){window.location='/Article/edit/id/'+article_id+'.html';}else{SSSUI.User.login();}
			}});
	        SSSUI.addkeyListener('d',{alt:1,ctrl:1,shift:1,cb:function(){
				if(uid>0){
	                if(confirm('删除?')){
	                    var d = dialog({}).show();
	                    $.getJSON('/Article/delete/id/'+article_id+'.html',function(res){
	                        if(res.status==1){
	                            d.content(res.info);
	                            setTimeout(function () {
	                                d.close().remove();
	                                $('article.hentry[data-id='+article_id+']').remove();
	                            }, 2000);
	                        }else{
	                            d.content(res.info);
	                        }
	                    })
	                }
				}else{SSSUI.User.login();}
            }});
		},
		Article_edit:function(){
			require.async('edit',function(e){e.init();})
		},
		Photo_index:function(){
			require('jquery.swipebox.min');
			var page =0,passed=false;
			function loadMeinv(){
				if(!passed){
					SSSUI.needPwd('661223',function(){
						passed=true;
						loadMeinv();
					},false);
				}else{
					page++;
	                $.getJSON('/Photo/getPhotoList.html',{p:page},function(data) {
	                    for(i in data){
	                        var h= ['',
	                                '<div class="pure-u-1-4">',
	                                '    <div class="info"><a class="pic" href="http://static.sssui.com/'+data[i].img+'" title="'+data[i].title+'"><img src="http://static.sssui.com/'+data[i].img+'-photo"></a>',
	                                '        <div class="title"><a href="http://static.sssui.com/'+data[i].img+'" target="_blank">'+data[i].title+'</a></div>',
	                                '    </div>',
	                                '</div>'].join('');
							$("#wrap").append(h);
	                    }
	                });
                }
			}
			loadMeinv();
			$(window).on("scroll",function(){
				$minUl = $("#wrap");
				if($minUl.height() <= $(window).scrollTop()+$(window).height()){
					//当最短的ul的高度比窗口滚出去的高度+浏览器高度大时加载新图片
					loadMeinv();
				}
			})
			$('.pic').swipebox();
		},
		Photo_read:function(){

		},
		Photo_add:function(){
			require('Photo');
		},
		Mylove_art:function(){
			$('body').append('<div id="overflow" class="overflow"></div>');
			$('body').css({overflow:'hidden'});
			SSSUI.needPwd('901205',function(){
				passed=true;
				$('#overflow').remove();
				$('body').css({overflow:'scroll'});
			},true);
			
			$('.entry-content .image').each(function(){
				$(this).append('<div class="viewlarge"><i class="fa fa-search"></i></div><div class="close"><i class="fa fa-remove"></i></div>');
			})
			$('.entry-content .image').mouseenter(function(){
				$(this).find('.viewlarge').animate({opacity:1});
			}).mouseleave(function(){
				$(this).find('.viewlarge').animate({opacity:0});
			}).find('.close').click(function(e) {
				console.info('cansnow');
				$(this).parents('.image').removeClass('large');
				$('body').css({overflow:'scroll'});
			});
			$('.entry-content .image').find('.viewlarge').click(function(e) {
				$(this).parents('.image').addClass('large');
				$('body').css({overflow:'hidden'});
			});
		},
		getPhotoList:function(page){
			$.getJSON('/Mylove/listphoto.html?pagesize=12&p='+(page || 1),function(data){
				var page=SSSUI.ShowPage(data.nowpage,12,data.total),
					list=data.list,
					html='',
					prev=data.nowpage-1,
					next=data.nowpage+1,
    				t=Math.ceil(data.total/12);
				for (var i = 0; i < list.length; i++) {
					html+='<li class="item"><a class="ui-grid-halve-img" href="/Mylove/viewphoto/id/'+list[i].id+'.html"><img src="http://static.sssui.com/'+list[i].img+'-photo?v='+STATIC_FILE_VERSION+'" alt=""></a></li>';
				}
				$('#image-navigation .nav-previous a').attr('data-page',prev<1 ? 'false' : prev);
				$('#image-navigation .nav-next a').attr('data-page',next>t ? 'false' : next);
				$('.pagination ul').html(page);
				$('#PhotoList').html(html+'<div class="clear"></div>');
			});
		},
		Mylove_photo:function(){			
			$('body').append('<div id="overflow" class="overflow"></div>');
			$('body').css({overflow:'hidden'});
			var page=1;
			//Base['needPwd']('901205',function(){
				passed=true;
				$('#overflow').remove();
				$('body').css({overflow:'scroll'});
				Base['getPhotoList'](page);
				$('.pagination ul').delegate('li a','click',function(){
					var p=$(this).attr('data-page');
					if(p){Base['getPhotoList'](p);}
				})
			//})
			function rs(){
				$('#image-navigation').css({
					position:'fixed',
					width:$('#main').width()+32,
					left:$('#main').offset().left-16,
					top:$(window).height()/2-16
				})
			}
			function scr(){
				var h=$('#image-navigation').css('top').replace('px',''),
					t=$('#primary').height()+$('#primary').offset().top-$(window).scrollTop()-30,
					d = h > t ? 'none' : 'block';
				$('#image-navigation').css({
					display:d
				})
			}
			$(window).resize(rs).trigger('resize');
			$(window).scroll(scr).trigger('scroll');
			$('#image-navigation a').click(function(){
				var page=$(this).attr('data-page');
				if(page=='false'){return false;}
				Base['getPhotoList'](page);
				return false;
			});
			require('beautify');
		    $(':radio').beautify();
		    $('#PhotoList').delegate('.ui-grid-halve-img', 'click', function(e) {
		    	var href=$(this).attr('href'),
			    	img=$(this).find('img'),
		    		src=$(this).find('img').attr('src').replace('http://static.sssui.com/ylh/','').replace(/-photo\?v=\d/,''),
		    		v=$('[name="editMode"]:checked').val();
		    	if(v==1){
					require.async('MylovePhoto',function(e){
						e.changeImg(src,function(src){
							img.attr('src','http://static.sssui.com/ylh/'+src+'-photo?v='+parseInt(Math.random()*999))
						});
					});
		        	return false;
		    	}else{

		    	}
		    });
		    $('#AddNewPhoto').click(function(){		    	
		    	var v=$('[name="editMode"]:checked').val();
		    	if(v==1){
					require.async('MylovePhoto',function(e){
						e.changeImg('',function(src,id){
							var ih='<li class="item"><a class="ui-grid-halve-img" href="/Mylove/viewphoto/id/'+id+'.html"><img src="http://static.sssui.com/'+src+'-photo?v=2" alt=""></a></li>';
							('#PhotoList').prepend(ih);
						});
					});
		    	}
		    })
		},
		Mylove_viewphoto:function(){
			function rs(){
				$('#image-navigation').css({
					position:'fixed',
					width:$('#main').width()+32,
					left:$('#main').offset().left-16,
					top:$(window).height()/2-16
				})
			}
			function scr(){
				var h=$('#image-navigation').css('top').replace('px',''),
					t=$('#primary').height()+$('#primary').offset().top-$(window).scrollTop()-30,
					d = h > t ? 'none' : 'block';
				$('#image-navigation').css({
					display:d
				})
			}
			$(window).resize(rs).trigger('resize');
			$(window).scroll(scr).trigger('scroll');
			$('#image-navigation a').click(function(){
				var href=$(this).attr('href');
				$.get(href,function(html){
					var src= $(html).find('[rel=attachment] img').attr('src');
					var phref= $(html).find('#image-navigation .nav-previous a').attr('href');
					var nhref= $(html).find('#image-navigation .nav-next a').attr('href');
					//console.info(src,phref,nhref);
					$('[rel=attachment] img').attr('src',src);
					$('#image-navigation .nav-previous a').attr('href',phref);
					$('#image-navigation .nav-next a').attr('href',nhref);
				})
				return false;
			})
		},
		Love_index:function(){
		    $('#AddNewPhoto').click(function(){		    	
		    	var v=$('[name="editMode"]:checked').val();
		    	if(v==1){
					require.async('LoveUpload',function(e){
						e.changeImg('',function(src,id){
							var ih='<li class="item"><a class="ui-grid-halve-img" href="/Mylove/viewphoto/id/'+id+'.html"><img src="http://static.sssui.com/'+src+'-photo?v=2" alt=""></a></li>';
							('#PhotoList').prepend(ih);
						});
					});
		    	}
		    })
			
		}
	}
	Base['article_tag']=Base['Article_index'];
	SSSUI.init();
	var n= Base[CONTROLLER_NAME+'_'+ACTION_NAME];
	//console.log(n);
	n && n();
	$.pjax.defaultOptions = {
		timeout : 2000,
		element : null,
		cache : 3600*24, // 缓存时间, 0为不缓存, 单位为秒
		storage : true, // 是否使用localstorage将数据保存到本地
		url : '', // 链接地址
		push : true,
		show : 'fade', // 展示的动画
		titleSuffix : ' - SSSUI - Cansnow\'s Blog', // 标题
		//titleSuffix : '',// 标题后缀
		type : 'GET',
		data : {
			pjax : true
		},
		dataType : 'html',
		callback : function(s){			
			//console.log(s);
			var n= Base[CONTROLLER_NAME+'_'+ACTION_NAME];
			n && n();
		}, // 回调函数
		// for jquery
		beforeSend : function(xhr) {
			$('#page').removeClass('danlie');
			//console.info(this);
			$(this.container).trigger('pjax.start', [ xhr, this ]);
			xhr && xhr.setRequestHeader('X-PJAX', true);
			$('#js-pjax-loader-bar').addClass('is-loading');
			$('#js-pjax-loader-bar .progress').css('width',"75%");
		},
		error : function() {
			this.callback && this.callback.call(this.element,{type:'error'});
			location.href = this.url;
		},
		complete : function(xhr) {
			$(this.container).trigger('pjax.end', [xhr,this]);
			$('#js-pjax-loader-bar .progress').css('width',"100%");
			$('#js-pjax-loader-bar').removeClass('is-loading');
			setTimeout(function(){
				$('#js-pjax-loader-bar .progress').css('width',"0%");
			},600);
		}
	};
	$.pjax({
        selector: "a[href][href!='#'][href!='javascript:void(0);'][href!='javascript:;'][pjax]",
        container: '#content', //内容替换的容器
    })
	Base['Article_index']();

});